import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../widget/dnp_date_picker.dart';

///时间选择弹窗
class DnpTimeChoiceDialog extends StatefulWidget {
  final String? curTime;
  final ValueChanged<DateTime>? onConfirm;

  const DnpTimeChoiceDialog({super.key, this.curTime, this.onConfirm});

  @override
  State<DnpTimeChoiceDialog> createState() => _DnpTimeChoiceDialogState();
}

class _DnpTimeChoiceDialogState extends State<DnpTimeChoiceDialog> {
  DateTime curDate = DateTime.now();

  @override
  void initState() {
    super.initState();
    List<String> parts = (widget.curTime ?? "12-08-1983").split('-');
    if (parts.length != 3) {
      parts = ["01", "01", "1970"];
    }
    int month = int.tryParse(parts[0]) ?? 1;
    int day = int.tryParse(parts[1]) ?? 1;
    int year = int.tryParse(parts[2]) ?? 1970;
    curDate = DateTime(year, month, day);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.transparent,
        body: Center(
            child: Container(
                width: 308.45,
                height: 431.6,
                padding: EdgeInsets.symmetric(horizontal: 14),
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage(
                            'assets/images/ic_identity_single_choice_bg.webp'),
                        fit: BoxFit.fill)),
                child: Column(children: [
                  SizedBox(height: 66),
                  Text(
                    "Date Selection",
                    style: TextStyle(
                        fontWeight: FontWeight.w600,
                        fontSize: 16,
                        color: Color(0xFF252630)),
                  ),
                  SizedBox(height: 14),
                  Container(
                    width: 267,
                    height: 248,
                    margin: EdgeInsets.only(right: 13),
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Expanded(
                              child: Text(
                                textAlign: TextAlign.center,
                                "day",
                                style: TextStyle(
                                    fontWeight: FontWeight.w500,
                                    fontSize: 14,
                                    color: Color(0xFF1E1E1E)),
                              ),
                            ),
                            Expanded(
                              child: Text(
                                textAlign: TextAlign.center,
                                "month",
                                style: TextStyle(
                                    fontWeight: FontWeight.w500,
                                    fontSize: 14,
                                    color: Color(0xFF1E1E1E)),
                              ),
                            ),
                            Expanded(
                              child: Text(
                                textAlign: TextAlign.center,
                                "years",
                                style: TextStyle(
                                    fontWeight: FontWeight.w500,
                                    fontSize: 14,
                                    color: Color(0xFF1E1E1E)),
                              ),
                            ),
                          ],
                        ),
                        const SizedBox(
                          height: 14,
                        ),
                        Expanded(
                          child: Stack(
                            children: [
                              Center(
                                child: Container(
                                  height: 40,
                                  width: double.infinity,
                                  decoration: const BoxDecoration(
                                    color: Color(0xFF1D4AC8),
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(20)),
                                  ),
                                ),
                              ),
                              CupertinoDatePicker(
                                mode: CupertinoDatePickerMode.date,
                                minimumYear: 1970,
                                maximumYear: DateTime.now().year,
                                initialDateTime: curDate,
                                onDateTimeChanged: (DateTime newDateTime) {
                                  curDate = newDateTime;
                                },
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  const SizedBox(
                    height: 14,
                  ),
                  _buildBottomButton()
                ]))));
  }

  Widget _buildBottomButton() {
    return Row(
      children: [
        Expanded(
            child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            Get.back();
          },
          child: Container(
            width: double.infinity,
            height: 48,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Color(0xFFCFE6FF),
              borderRadius: BorderRadius.circular(16),
            ),
            child: Text(
              "Back",
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: Color(0xFF95A5B9),
              ),
            ),
          ),
        )),
        const SizedBox(
          width: 9,
        ),
        Expanded(
            child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            widget.onConfirm?.call(curDate);
            Get.back();
          },
          child: Container(
            width: double.infinity,
            height: 48,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Color(0xFF1D4AC8),
              borderRadius: BorderRadius.circular(16),
            ),
            child: Text(
              "Confirm",
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: Colors.white,
              ),
            ),
          ),
        )),
        const SizedBox(
          width: 13,
        ),
      ],
    );
  }
}
